import java.util.*;
import java.text.*;
import java.io.*;
/**
 * No copyright.
 * Class to maintain event information and track students.
 * @author Quan Nguyen
 * @version 1.02 2008-July-24
 */
public class Event implements Comparable<Event>
{
    private String name;            //Name of event
    private int id;                 //Internal id number for event
    private Date date; //Date of event
    private Category category;        //Category for event (Drill, Cleanup, etc.)
    private Hashtable<Integer, Integer> table;          //Table containing all students involved
    
    private String dataName;        //Name of data (Hours, Performances, etc.)
    private int countable;           //Number of ____ required to obtain award
    private Event parentEvent;        //Is child of FieldMeet, its parent
    private String S = File.separator;
    
    /**
     * Constructs a standard event with name, time, and category.
     * @param nam name of event
     * @param dat date of event (Date)
     * @param cat category of event
     * @param datnam name of the data
     * @param count amount of <insert name of data> required to earn award
     */
    public Event(String nam, Date dat, String cat, String datnam, int count)
    {
        name = nam;
        date = dat;
        category = new Category(cat);
        dataName = datnam;
        table = loadEventTable();
        countable = count;
        parentEvent = null;
    }
    
    public Event(String nam, Date dat, String cat, String datnam, int count, Event parent)
    {
        this(nam, dat, cat, datnam, count);
        parentEvent = parent;
    }
    
    public String getName()
    {
        return name;
    }
    
    public Date getDate()
    {
        return date;
    }
    
    public String getDateString()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("dd MMM yyyy");
        return sdf.format(date);
    }
    
    public Category getCategory()
    {
        return category;
    }
    
    public String getDataName()
    {
        return dataName;
    }
    
    public int getCountable()
    {
        return countable;
    }
    
    public Hashtable<Integer, Integer> getTable()
    {
        return table;
    }
    
//    public void addStudent(Student stu, int data)
//    {
//        if(!table.containsKey(stu))
//            table.put(new Integer(stu.getID()), new Integer(data));
//    }
//    
//     public void addStudent(Student stu)
//     {
//         addStudent(stu, new Integer(0));
//     }
//     
//     public void editStudent(Student stu, int data)
//     {
//         if(table.containsKey(stu))
//             table.put(new Integer(stu.getID()), new Integer(data));
//     }
//     
//     public void removeStudent(Student stu)
//     {
//         if(table.containsKey(new Integer(stu.getID())))
//             table.remove(stu);
//     }
    
    public String toString()
    {
        return (name + " " + getDateString());
    }
    
    public int compareTo(Event that)
    {
        Date otherDate = ((Event)that).getDate();
        return date.compareTo(otherDate);
    }
    
    private Hashtable<Integer, Integer> loadEventTable()
    {
        try
        {
            File directory = new File("libraries"+S+"rosters"+S);
            File file = new File(directory, this.toString()+".atr");
            if(!file.exists())
            {
                file.createNewFile();
                PrintStream writer = new PrintStream(new FileOutputStream(file));
                writer.println("ID,count"); //prepares the newly created file
                writer.close();
            }
            if(file.canRead())
            {
                return RosterReader.readRoster(file);
            } 
        }
        catch (Exception e) { e.printStackTrace();}
        return new Hashtable<Integer, Integer>();
    }
    
    public Event getParent()
    {
        return parentEvent;
    }
       
}

//a utility class that identifies the categories so that it will be standard across all event types
class Category
{
    public static final String[] CATEGORIES = {"NONE", "FIELD MEET", "DRILL", "ACADEMICS", "COLOR GUARD", 
                                                "PT", "ORIENTEERING",  "MARKSMANSHIP", "UNIT SERVICE", "COMMUNITY SERVICE",
                                                "PARTICIPATION", "MINI BOOT CAMP", "SEA CRUISE", "OTHER"};
    private String myCategory;
    
    public Category()
    {
        myCategory = CATEGORIES[0];
    }
    
    public Category(String str)
    {
        setCategory(str);
    }
    
    public void setCategory(String str)
    {
        for(int i = 0; i < CATEGORIES.length; i++)
        {
            if(str.toUpperCase().equals(CATEGORIES[i]))
                myCategory = CATEGORIES[i];
        }
    }
    
    public String getCategory()
    {
        return myCategory;
    }
    
    public int getIndex() //for use by the ComboBox in AdminTracker
    {
        for(int i = 0; i < CATEGORIES.length; i++)
        {
            if(myCategory.toUpperCase().equals(CATEGORIES[i]))
                return i;
        }
        return CATEGORIES.length-1;
    }
    
    public String toString()
    {
        return myCategory;
    }
    
    public boolean equals(Object that)
    {
        return this.myCategory.equals(((Category)that).getCategory());
    }
}